'use strict';

const { logger } = require('ee-core/log');
const { settingsService } = require('../service/settings');

/**
 * 系统设置控制器
 * @class
 */
class SettingsController {

  /**
   * 获取用户设置
   */
  async getSettings(args) {
    try {
      const { userId = 'default' } = args;
      const settings = await settingsService.getSettings(userId);
      
      logger.info('获取用户设置成功:', settings);
      return {
        code: 200,
        message: '获取设置成功',
        data: settings
      };
    } catch (error) {
      logger.error('获取用户设置失败:', error);
      return {
        code: 500,
        message: '获取设置失败',
        error: error.message
      };
    }
  }

  /**
   * 保存用户设置
   */
  async saveSettings(args) {
    try {
      const { userId = 'default', settings } = args;
      
      if (!settings || typeof settings !== 'object') {
        return {
          code: 400,
          message: '设置参数无效'
        };
      }
      
      const result = await settingsService.saveSettings(userId, settings);
      
      logger.info('保存用户设置成功:', settings);
      return {
        code: 200,
        message: '设置保存成功',
        data: result
      };
    } catch (error) {
      logger.error('保存用户设置失败:', error);
      return {
        code: 500,
        message: '保存设置失败',
        error: error.message
      };
    }
  }

  /**
   * 重置用户设置
   */
  async resetSettings(args) {
    try {
      const { userId = 'default' } = args;
      const defaultSettings = settingsService.getDefaultSettings();
      
      await settingsService.saveSettings(userId, defaultSettings);
      
      logger.info('重置用户设置成功');
      return {
        code: 200,
        message: '设置重置成功',
        data: defaultSettings
      };
    } catch (error) {
      logger.error('重置用户设置失败:', error);
      return {
        code: 500,
        message: '重置设置失败',
        error: error.message
      };
    }
  }
}

SettingsController.toString = () => '[class SettingsController]';

module.exports = SettingsController;
